﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Reflection;

namespace TS_XY_Train_Repaire.UserControll.Locomotive.Report.Train
{
    public partial class xuc_Train_Report_ByName_FunctionButton_Vertical : DevExpress.XtraEditors.XtraUserControl
    {
        private TS_XY_Train_Repaire.Pub.ProgressBarForm _pbf = new TS_XY_Train_Repaire.Pub.ProgressBarForm();
        private delegate void funHandle(int nValue);
        private funHandle myHandle = null;
        public WIN_RegisterReport_OBJ _obj = new WIN_RegisterReport_OBJ();
        public DevExpress.XtraEditors.XtraForm _PForm = new XtraForm();
        public xuc_Train_Report_ByName_FunctionButton_Vertical()
        {
            InitializeComponent();
        }

        private void xuc_Train_Report_ByName_FunctionButton_Vertical_Load(object sender, EventArgs e)
        {
            WIN_RegisterReportControll_OBJ _c_obj = this.Tag as WIN_RegisterReportControll_OBJ;
            _PForm = this.Parent as DevExpress.XtraEditors.XtraForm;
            _obj = this.Parent.Tag as WIN_RegisterReport_OBJ;
            TS_Win_Class.GetRepotAuthority(_obj, _PForm);
            foreach (Control _cl in _PForm.Controls)
            {
                if (_cl.GetType().ToString() == "System.Windows.Forms.Panel")
                {
                    Panel pn = _cl as Panel;
                    foreach (Control item in pn.Controls)
                    {
                        if (item.GetType().ToString() == "System.Windows.Forms.TextBox")
                        {
                            if (item.Name.IndexOf("gf_data") >= 0)
                            {
                                item.TextChanged += new EventHandler(gf_data_TextChanged);
                            }
                        }
                    }

                }
                else
                {
                    if (_cl.GetType().ToString() == "System.Windows.Forms.TextBox")
                    {
                        if (_cl.Name.IndexOf("gf_data") >= 0)
                        {
                            _cl.TextChanged += new EventHandler(gf_data_TextChanged);
                        }
                    }
                }
            }
            if (TS_Win_Class.GetSQlFieldValue("select count(*) as f_count from TS_S_TR_DataFinish_Authority where f_del=0 and ((f_type=0 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ") or (f_type=1 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserPosID + ") or (f_type=2 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + "))", "f_count") == "0")
            {
                bt_Finish.Visible = false;
            }
            else
            {
                bt_Finish.Visible = true;
            }
            if (TS_Win_Class.GetSQlFieldValue("select count(*) as f_count from TS_S_T_H_RM_Result where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID, "f_count") == "0")
            {
                string sql = "select f_ttname,f_name,(select f_name from TS_S_T_P_Train_RP where f_del=0 and f_id=TS_TR_BU_Work_Main.f_rpid) as f_rpname,f_rpid from TS_TR_BU_Work_Main where f_del=0 and f_id=" + _obj._sBMWID;
                SqlConnection con = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                con.Open();
                SqlCommand com = new SqlCommand(sql, con);
                SqlDataReader raed = com.ExecuteReader();
                while (raed.Read())
                {
                    try
                    {
                        if (_c_obj._sTrainNameField.Length > 0)
                        {
                            _PForm.Controls.Find(_c_obj._sTrainNameField, true)[0].Text = raed["f_ttname"].ToString() + raed["f_name"].ToString();
                        }
                        if (_c_obj._sRPNameField.Length > 0)
                        {
                            _PForm.Controls.Find(_c_obj._sRPNameField, true)[0].Text = raed["f_rpname"].ToString();
                        }
                    }
                    catch { }
                }
                raed.Close();
                con.Close();
                con.Dispose();
                bt_AutoSum.Enabled = true;
                bt_AutoSum.ForeColor = Color.DodgerBlue;
                bt_CreatReport.Enabled = true;
                bt_CreatReport.ForeColor = Color.DodgerBlue;
                bt_Announce.Enabled = false;
                bt_Announce.ForeColor = Color.DodgerBlue;
                bt_Finish.Enabled = false;
                bt_Finish.ForeColor = Color.Gray;
                bt_Preview.Enabled = false;
                bt_Preview.ForeColor = Color.Gray;
                bt_Print.Enabled = false;
                bt_Print.ForeColor = Color.Gray;
                bt_EXImg.Enabled = false;
                bt_EXImg.ForeColor = Color.Gray;
                bt_EXPDF.Enabled = false;
                bt_EXPDF.ForeColor = Color.Gray;
                bt_CreatReport.Text = "生成报表";
            }
            else
            {
                bt_AutoSum.Enabled = true;
                bt_AutoSum.ForeColor = Color.DodgerBlue;
                bt_CreatReport.Enabled = true;
                bt_CreatReport.ForeColor = Color.Blue;
                bt_Finish.Enabled = true;
                bt_Finish.ForeColor = Color.Red;
                bt_Preview.Enabled = true;
                bt_Preview.ForeColor = Color.Green;
                bt_Print.Enabled = true;
                bt_Print.ForeColor = Color.Green;
                bt_EXImg.Enabled = true;
                bt_EXImg.ForeColor = Color.Green;
                bt_EXPDF.Enabled = true;
                bt_EXPDF.ForeColor = Color.Green;
                bt_CreatReport.Text = "修改报表";
                if (TS_Win_Class.GetSQlFieldValue("select f_func_2 from TS_S_T_P_Tree where f_id=" + _obj._sRMID, "f_func_2") == "1")
                {
                    bt_Announce.Enabled = true;
                    bt_Announce.ForeColor = Color.Green;
                }
                else
                {
                    bt_Announce.Enabled = false;
                    bt_Announce.ForeColor = Color.Gray;
                }
                string strSQL = "select * from TS_S_T_H_RM_Result where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID;
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand comm = new SqlCommand(strSQL, conn);
                SqlDataReader read = comm.ExecuteReader();
                TextBox _tb = new TextBox();
                while (read.Read())
                {
                    try
                    {
                        ThoughtWorks.QRCode.Codec.QRCodeEncoder _qr = new ThoughtWorks.QRCode.Codec.QRCodeEncoder();
                        if (_PForm.Controls.Find("pb_Code", true).Length > 0)
                        {
                            PictureBox _pb = new PictureBox();
                            (_PForm.Controls.Find("pb_Code", true)[0] as PictureBox).Image = _qr.Encode(read["f_guid"].ToString());
                        }
                    }
                    catch
                    { }
                    if (read["f_isover"].ToString() == "1")
                    {
                        bt_AutoSum.Visible = false;
                        bt_CreatReport.Visible = false;
                        bt_Finish.Visible = false;
                        bt_Announce.Visible = false;
                    }
                    _tb = new TextBox();
                    for (int i = 0; i < 170; i++)
                    {
                        try
                        {
                            if (_PForm.Controls.Find("f_data" + i.ToString(), true)[0].GetType().ToString() == "System.Windows.Forms.TextBox")
                            {
                                _tb = new TextBox();
                                _tb = (TextBox)_PForm.Controls.Find("f_data" + i.ToString(), true)[0];
                                _tb.Text = read["f_data" + i.ToString()].ToString();
                            }
                        }
                        catch { }
                    }
                }
                read.Close();
                conn.Close();
                conn.Dispose();
            }
        }

        private void ShowProgressBar()
        {
            myHandle = new funHandle(_pbf.SetProgressValue);
            _pbf.ShowDialog();
        }
        private void bt_AutoSum_Click(object sender, EventArgs e)
        {
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(_AutoSum));
            if (cb_0.Checked)
            {
                thread = new System.Threading.Thread(new System.Threading.ThreadStart(_AutoSumShow));
            }
            _pbf.iMaxValue = Convert.ToInt32(TS_Win_Class.GetSQlFieldValue("select count(*) as f_count from TS_S_T_P_Report_Field where f_del=0 and f_rmid=" + _obj._sRMID, "f_count"));
            thread.Start();
        }
        private void _AutoSum()
        {
            if (MessageBox.Show("确定从现场作业项点中生成工作数据吗？", "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                MethodInvoker mi = new MethodInvoker(ShowProgressBar);
                this.BeginInvoke(mi);
                int i = 0;
                System.Threading.Thread.Sleep(10); // sleep to show window  

                DevExpress.XtraEditors.XtraForm _PForm = this.Parent as DevExpress.XtraEditors.XtraForm;
                _obj = this.Parent.Tag as WIN_RegisterReport_OBJ;
                string strSQL = "select f_id,f_name,f_fieldname,f_resulttype,f_type from TS_S_T_P_Report_Field where f_del=0 and f_rmid=" + _obj._sRMID;
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand comm = new SqlCommand(strSQL, conn);
                SqlDataReader read = comm.ExecuteReader();
                TextBox _tb = new TextBox();
                while (read.Read())
                {
                    i++;
                    _pbf.sName = read["f_name"].ToString();
                    if (_PForm.Controls.Find(read["f_fieldname"].ToString(), true).Length == 0)
                    {
                        MessageBox.Show("数据填报界面与系统设置不匹配，请联系系统开发！！");
                        bt_CreatReport.Enabled = false;
                        bt_CreatReport.ForeColor = Color.Gray;
                        break;
                    }
                    else
                    {
                        if (read["f_type"].ToString() == "4" || read["f_type"].ToString() == "7" || read["f_type"].ToString() == "9" || read["f_type"].ToString() == "11" || read["f_type"].ToString() == "13" || read["f_type"].ToString() == "15")
                        {
                            _tb = (TextBox)_PForm.Controls.Find(read["f_fieldname"].ToString(), true)[0];
                        }
                        else
                        {
                            _tb = (TextBox)_PForm.Controls.Find(read["f_fieldname"].ToString(), true)[0];
                        }
                        string _filedaname = "";
                        string _filedlist = "";
                        string _queryresult = "";
                        if (read["f_resulttype"].ToString() == "0")//自动计算结果
                        {
                            _tb.Enabled = true;
                            switch (read["f_type"].ToString())
                            {
                                case "0":
                                    _filedaname = "f_id,f_bdata1";
                                    _filedlist = "f_id,f_bdata1";
                                    break;
                                case "1":
                                    _filedaname = "f_id,f_result";
                                    _filedlist = "f_id,f_result";
                                    break;
                                case "2":
                                    _filedaname = "f_id,f_result";
                                    _filedlist = "f_id,f_result";
                                    break;
                                case "3":
                                    //                                    _filedaname = "f_id,f_stuffid,(select f_workercode from TS_S_T_P_Stuff where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_stuffid) as f_workercode,(select f_name from TS_S_T_P_Stuff where f_id=TS_TR_BU_Work_Main_Group_WorkPlace.f_stuffid) as f_name";
                                    _filedaname = "f_id,f_stuffid,(select f_workercode from TS_S_T_P_Stuff where f_id=t.f_stuffid) as f_workercode,isnull((select stuff((select '/'+f_name from ts_s_t_p_stuff where f_id in (select f_fid from TS_TR_BU_Work_Main_Relation where f_del=0 and f_ftype=0 and f_rtype=4 and f_rid in (select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_id=t.f_id)) for xml path('')) , 1 , 1 , '')),' ') as f_name";
                                    _filedlist = "f_id,f_name,f_stuffid,f_workercode";
                                    break;
                                case "4":
                                    _filedaname = "f_id,CONVERT(varchar(16), f_time0, 120) as f_time";
                                    _filedlist = "f_id,f_time";
                                    break;
                                case "5":
                                    _filedaname = "f_id,(select f_name from TS_S_T_P_Department where f_id=(select f_depid from TS_S_T_P_Stuff where f_id=t.f_stuffid)) as f_depname,(select f_depid from TS_S_T_P_Stuff where f_id=t.f_stuffid) as f_depid";
                                    _filedlist = "f_id,f_depname,f_depid";
                                    break;
                                case "6":
                                    _filedaname = "f_id,f_stuffid1,(select f_workercode from TS_S_T_P_Stuff where f_id=t.f_stuffid1) as f_workercode,isnull((select stuff((select '/'+f_name from ts_s_t_p_stuff where f_id in (select f_fid from TS_TR_BU_Work_Main_Relation where f_del=0 and f_ftype=0 and f_rtype=5 and f_rid in (select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_id=t.f_id)) for xml path('')) , 1 , 1 , '')),' ') as f_name";
                                    _filedlist = "f_id,f_name,f_stuffid1,f_workercode";
                                    break;
                                case "7":
                                    _filedaname = "f_id,CONVERT(varchar(16), f_time1, 120) as f_time1";
                                    _filedlist = "f_id,f_time1";
                                    break;
                                case "8":
                                    _filedaname = "f_id,f_stuffid2,(select f_workercode from TS_S_T_P_Stuff where f_id=t.f_stuffid2) as f_workercode,isnull((select stuff((select '/'+f_name from ts_s_t_p_stuff where f_id in (select f_fid from TS_TR_BU_Work_Main_Relation where f_del=0 and f_ftype=0 and f_rtype=6 and f_rid in (select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_id=t.f_id)) for xml path('')) , 1 , 1 , '')),' ') as f_name";
                                    _filedlist = "f_id,f_name,f_stuffid2,f_workercode";
                                    break;
                                case "9":
                                    _filedaname = "f_id,CONVERT(varchar(16), f_time2, 120) as f_time2";
                                    _filedlist = "f_id,f_time2";
                                    break;
                                case "10":
                                    _filedaname = "f_id,f_stuffid3,(select f_workercode from TS_S_T_P_Stuff where f_id=t.f_stuffid3) as f_workercode,isnull((select stuff((select '/'+f_name from ts_s_t_p_stuff where f_id in (select f_fid from TS_TR_BU_Work_Main_Relation where f_del=0 and f_ftype=0 and f_rtype=7 and f_rid in (select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_id=t.f_id)) for xml path('')) , 1 , 1 , '')),' ') as f_name";
                                    _filedlist = "f_id,f_name,f_stuffid3,f_workercode";
                                    break;
                                case "11":
                                    _filedaname = "f_id,CONVERT(varchar(16), f_time3, 120) as f_time3";
                                    _filedlist = "f_id,f_time3";
                                    break;
                                case "12":
                                    _filedaname = "f_id,f_stuffid4,(select f_workercode from TS_S_T_P_Stuff where f_id=t.f_stuffid4) as f_workercode,isnull((select stuff((select '/'+f_name from ts_s_t_p_stuff where f_id in (select f_fid from TS_TR_BU_Work_Main_Relation where f_del=0 and f_ftype=0 and f_rtype=8 and f_rid in (select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_id=t.f_id)) for xml path('')) , 1 , 1 , '')),' ') as f_name";
                                    _filedlist = "f_id,f_name,f_stuffid4,f_workercode";
                                    break;
                                case "13":
                                    _filedaname = "f_id,CONVERT(varchar(16), f_time4, 120) as f_time4";
                                    _filedlist = "f_id,f_time4";
                                    break;
                                case "14":
                                    _filedaname = "f_id,f_stuffid5,(select f_workercode from TS_S_T_P_Stuff where f_id=t.f_stuffid5) as f_workercode,isnull((select stuff((select '/'+f_name from ts_s_t_p_stuff where f_id in (select f_fid from TS_TR_BU_Work_Main_Relation where f_del=0 and f_ftype=0 and f_rtype=9 and f_rid in (select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_id=t.f_id)) for xml path('')) , 1 , 1 , '')),' ') as f_name";
                                    _filedlist = "f_id,f_name,f_stuffid5,f_workercode";
                                    break;
                                case "15":
                                    _filedaname = "f_id,CONVERT(varchar(16), f_time5, 120) as f_time5";
                                    _filedlist = "f_id,f_time5";
                                    break;
                            }
                            _queryresult = " and f_mwwpid in (select f_pwpid from TS_S_T_P_Report_Field_WP where f_del=0 and f_rmfid=" + read["f_id"].ToString() + ") and f_wpid in (select f_wpid from TS_S_T_P_Report_Field_WP where f_del=0 and f_rmfid=" + read["f_id"].ToString() + ") and f_bmwid=" + _obj._sBMWID;// + " and f_bmwdid=" + _obj._sFunctionID;
                            try
                            {
                                string[] _Result;
                                _Result = TS_Win_Class.GetSQlFieldValue("select top 1 " + _filedaname + " from TS_TR_BU_Work_Main_Group_WorkPlace t where f_del=0" + _queryresult, _filedlist.Split(','));
                                if (read["f_type"].ToString() == "4" || read["f_type"].ToString() == "7" || read["f_type"].ToString() == "9" || read["f_type"].ToString() == "11" || read["f_type"].ToString() == "13" || read["f_type"].ToString() == "15")
                                {
                                    if (_Result[0] != "-1")
                                    {
                                        _tb.Text = _Result[1];
                                        _tb.Tag = _Result[0];
                                    }
                                    else
                                    {
                                        _tb.Text = " ";
                                    }
                                }
                                else
                                {
                                    if (_Result[0] != "-1")
                                    {
                                        _tb.Text = _Result[1];
                                        _tb.Tag = _Result[0];
                                    }
                                    else
                                    {
                                        _tb.Text = " ";
                                    }
                                }
                            }
                            catch
                            {
                                switch (read["f_type"].ToString())
                                {
                                    case "0":
                                        _tb.Text = "0";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "1":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "2":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "3":
                                        _tb.Text = " ";
                                        _tb.Tag = "-1";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "4":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "5":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "6":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "7":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "8":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "9":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "10":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "11":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "12":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "13":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "14":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "15":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                }
                            }
                        }
                        else if (read["f_resulttype"].ToString() == "1")//手动填报数据
                        {
                            _tb.Enabled = true;
                        }
                        else if (read["f_resulttype"].ToString() == "2")//取主表数据
                        {
                            _tb.Enabled = true;
                            switch (read["f_type"].ToString())
                            {
                                case "0":
                                    _filedaname = "f_id,f_name";
                                    _filedlist = "f_id,f_name";
                                    break;
                                case "1":
                                    _filedaname = "f_id,f_brief";
                                    _filedlist = "f_id,f_brief";
                                    break;
                                case "2":
                                    _filedaname = "f_id,f_brief";
                                    _filedlist = "f_id,f_brief";
                                    break;
                                case "3":
                                    _filedaname = "f_id,f_stuffid,(select f_workercode from TS_S_T_P_Stuff where f_id=TS_TR_BU_Work_Main.f_stuffid) as f_workercode,(select f_name from TS_S_T_P_Stuff where f_id=TS_TR_BU_Work_Main.f_stuffid) as f_name";
                                    _filedlist = "f_id,f_name,f_stuffid,f_workercode";
                                    break;
                                case "4":
                                    _filedaname = "f_id,f_etime";
                                    _filedlist = "f_id,f_etime";
                                    break;
                                case "5":
                                    _filedaname = "f_id,(select f_name from TS_S_T_P_Department where f_id=(select f_depid from TS_S_T_P_Stuff where f_id=TS_TR_BU_Work_Main.f_stuffid)) as f_depname,(select f_depid from TS_S_T_P_Stuff where f_id=TS_TR_BU_Work_Main.f_stuffid) as f_depid";
                                    _filedlist = "f_id,f_depname,f_depid";
                                    break;
                                default:
                                    break;
                            }
                            try
                            {
                                string[] _Result;
                                _Result = TS_Win_Class.GetSQlFieldValue("select top 1 " + _filedaname + " from TS_TR_BU_Work_Main where f_del=0 and f_id=" + _obj._sBMWID, _filedlist.Split(','));
                                _tb.Text = _Result[1];
                                _tb.Tag = _Result[0];
                            }
                            catch
                            {
                                switch (read["f_type"].ToString())
                                {
                                    case "0":
                                        _tb.Text = "0";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "1":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "2":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "3":
                                        _tb.Text = " ";
                                        _tb.Tag = "-1";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "4":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                    case "5":
                                        _tb.Text = "";
                                        _tb.BackColor = Color.Red;
                                        break;
                                }
                            }
                        }
                    }
                    System.Threading.Thread.Sleep(10);
                    // 这里直接调用代理  
                    this.Invoke(this.myHandle, new object[] { (i) });
                }
                read.Close();
                conn.Close();
                conn.Dispose();
                bt_CreatReport.Enabled = true;
                bt_CreatReport.ForeColor = Color.Blue;
            }
        }
        private void _AutoSumShow()
        {
            if (MessageBox.Show("确定从现场作业项点中生成工作数据吗？", "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                MethodInvoker mi = new MethodInvoker(ShowProgressBar);
                this.BeginInvoke(mi);
                int i = 0;
                System.Threading.Thread.Sleep(10); // sleep to show window  

                DevExpress.XtraEditors.XtraForm _PForm = this.Parent as DevExpress.XtraEditors.XtraForm;
                _obj = this.Parent.Tag as WIN_RegisterReport_OBJ;
                string strSQL = "select f_id,f_name,f_fieldname,f_resulttype,f_type from TS_S_T_P_Report_Field where f_del=0 and f_rmid=" + _obj._sRMID;
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand comm = new SqlCommand(strSQL, conn);
                SqlDataReader read = comm.ExecuteReader();
                TextBox _tb = new TextBox();
                while (read.Read())
                {
                    i++;
                    _pbf.sName = read["f_name"].ToString();
                    if (_PForm.Controls.Find(read["f_fieldname"].ToString(), true).Length == 0)
                    {
                        MessageBox.Show("数据填报界面与系统设置不匹配，请联系系统开发！！");
                        bt_CreatReport.Enabled = false;
                        bt_CreatReport.ForeColor = Color.Gray;
                        break;
                    }
                    else
                    {
                        if (read["f_type"].ToString() == "4" || read["f_type"].ToString() == "7" || read["f_type"].ToString() == "9" || read["f_type"].ToString() == "11" || read["f_type"].ToString() == "13" || read["f_type"].ToString() == "15")
                        {
                            _tb = (TextBox)_PForm.Controls.Find(read["f_fieldname"].ToString(), true)[0];
                        }
                        else
                        {
                            _tb = (TextBox)_PForm.Controls.Find(read["f_fieldname"].ToString(), true)[0];
                        }
                        if (read["f_type"].ToString() == "4" || read["f_type"].ToString() == "7" || read["f_type"].ToString() == "9" || read["f_type"].ToString() == "11" || read["f_type"].ToString() == "13" || read["f_type"].ToString() == "15")
                        {
                            _tb.BackColor = Color.Green;
                        }
                        else
                        {
                            _tb.BackColor = Color.Green;
                        }

                    }
                    System.Threading.Thread.Sleep(80);
                    // 这里直接调用代理  
                    this.Invoke(this.myHandle, new object[] { (i) });
                }
                read.Close();
                conn.Close();
                conn.Dispose();
                bt_CreatReport.Enabled = true;
                bt_CreatReport.ForeColor = Color.Blue;
            }
        }
        private void bt_CreatReport_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定将填报数据存入报表中吗？", "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                _PForm = this.Parent as DevExpress.XtraEditors.XtraForm;
                string _field = "f_bmwid,f_rmid,f_type";
                bool _isEmpty = true;
                string _value = _obj._sBMWID + "," + _obj._sRMID + ",0";
                string _Update = "";

                foreach (Control _cl in _PForm.Controls)
                {
                    if (_cl.GetType().ToString() == "System.Windows.Forms.Panel")
                    {
                        Panel pn = _cl as Panel;
                        foreach (Control item in pn.Controls)
                        {
                            if (item.GetType().ToString() == "System.Windows.Forms.TextBox")
                            {
                                if ((item as TextBox).Name.Substring(0, 1) == "f")
                                {

                                    _field += "," + (item as TextBox).Name;
                                    _value += ",'" + (item as TextBox).Text.Trim() + "'";
                                    _Update += "," + (item as TextBox).Name + "='" + (item as TextBox).Text.Trim() + "'";
                                    if ((item as TextBox).Text.Trim().Length > 0)
                                    {
                                        _isEmpty = true;
                                    }
                                }
                            }
                        }

                    }
                    else
                    {
                        if (_cl.GetType().ToString() == "System.Windows.Forms.TextBox")
                        {
                            if ((_cl as TextBox).Name.Substring(0, 1) == "f")
                            {

                                _field += "," + (_cl as TextBox).Name;
                                _value += ",'" + (_cl as TextBox).Text.Trim() + "'";
                                _Update += "," + (_cl as TextBox).Name + "='" + (_cl as TextBox).Text.Trim() + "'";
                                if ((_cl as TextBox).Text.Trim().Length > 0)
                                {
                                    _isEmpty = true;
                                }
                            }
                        }
                    }
                }
                if (_isEmpty)
                {
                    if (bt_CreatReport.Text != "修改报表")
                    {

                        string sql = "insert into TS_S_T_H_RM_Result (" + _field + ") values(" + _value + ")";
                        string _Guid= TS_Win_Class.MySqlComGetID(sql);
                        try
                        {
                            ThoughtWorks.QRCode.Codec.QRCodeEncoder _qr = new ThoughtWorks.QRCode.Codec.QRCodeEncoder();
                            if (_PForm.Controls.Find("pb_Code", true).Length > 0)
                            {
                                PictureBox _pb = new PictureBox();
                                (_PForm.Controls.Find("pb_Code", true)[0] as PictureBox).Image = _qr.Encode(TS_Win_Class.GetSQlFieldValue("select f_guid from TS_S_T_H_RM_Result where f_id =" + _Guid, "f_guid"));
                            }
                        }
                        catch
                        { }

                        MessageBox.Show("数据生成成功！");
                        bt_AutoSum.Enabled = true;
                        bt_AutoSum.ForeColor = System.Drawing.SystemColors.HotTrack;
                        bt_CreatReport.Enabled = true;
                        bt_CreatReport.ForeColor = Color.Blue;
                        bt_Finish.Enabled = true;
                        bt_Finish.ForeColor = Color.Red;
                        bt_Preview.Enabled = true;
                        bt_Preview.ForeColor = Color.Green;
                        bt_Print.Enabled = true;
                        bt_Print.ForeColor = Color.Green;
                        bt_EXImg.Enabled = true;
                        bt_EXImg.ForeColor = Color.Green;
                        bt_EXPDF.Enabled = true;
                        bt_EXPDF.ForeColor = Color.Green;
                        bt_CreatReport.Text = "修改报表";
                        if (TS_Win_Class.GetSQlFieldValue("select f_func_2 from TS_S_T_P_Tree where f_id=" + _obj._sRMID, "f_func_2") == "1")
                        {
                            bt_Announce.Enabled = true;
                            bt_Announce.ForeColor = Color.Green;
                        }
                        else
                        {
                            bt_Announce.Enabled = false;
                            bt_Announce.ForeColor = Color.Gray;
                        }
                    }
                    else
                    {
                        string SQL = "update TS_S_T_H_RM_Result set f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate()" + _Update + " where  f_del=0 and f_type=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID;
                        TS_Win_Class.MySqlCom(SQL);
                        MessageBox.Show("数据修改成功！");
                    }
                }
                else
                {
                    MessageBox.Show("手动填写数据不能为空！");
                }
            }
        }

        private void bt_Finish_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("数据封闭后将不能修改任何数据，确定封闭数据吗？", "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                _PForm = this.Parent as DevExpress.XtraEditors.XtraForm;
                string _ReportFileName = TS_Win_Class.GetSQlFieldValue("select (select f_workcode from TS_TR_BU_Work_Main where f_id=TS_S_T_H_RM_Result.f_bmwid)+(select f_name from TS_S_T_P_Report_Main where f_id=TS_S_T_H_RM_Result.f_rmid) as f_name from TS_S_T_H_RM_Result where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID, "f_name");
                if (!Directory.Exists(TS_Win_Class._EXP_Direct))
                {

                    Directory.CreateDirectory(TS_Win_Class._EXP_Direct);

                }
                if (File.Exists(TS_Win_Class._EXP_Direct + _ReportFileName + ".png"))
                {
                    File.Delete(TS_Win_Class._EXP_Direct + _ReportFileName + ".png");
                }
                Assembly assembly = Assembly.GetExecutingAssembly();            // 实例化窗体
                DevExpress.XtraReports.UI.XtraReport _Report = assembly.CreateInstance(TS_Win_Class.GetSQlFieldValue("select f_reportname from TS_S_T_P_Report_Main where f_id=" + _obj._sRMID, "f_reportname")) as DevExpress.XtraReports.UI.XtraReport;
                DataTable _dt = new DataTable();
                TS_Win_Class.FilledDataTable("select * from TS_S_T_H_RM_Result where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID, _dt);
                _Report.DataSource = _dt;
                _Report.ExportToImage(TS_Win_Class._EXP_Direct + _ReportFileName + ".png");

                string SQL = "update TS_S_T_H_RM_Result set f_isover=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID;
                TS_Win_Class.MySqlCom(SQL);

                FileStream fs = new FileStream(TS_Win_Class._EXP_Direct + _ReportFileName + ".png", FileMode.Open, FileAccess.Read);
                System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                string base64String = Convert.ToBase64String(bt);
                br.Close();
                fs.Close();
                string strSQL = "insert into TS_S_T_H_RM_Result_Resource(f_mainid,f_brief,f_filename,f_workerid) values(" + TS_Win_Class.GetSQlFieldValue("select top 1 f_id from TS_S_T_H_RM_Result where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID, "f_id") + ",'" + base64String + "','" + TS_Win_Class._EXP_Direct + _ReportFileName + ".png'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                TS_Win_Class.MySqlCom(strSQL);
                bt_AutoSum.Visible = false;
                bt_CreatReport.Visible = false;
                bt_Finish.Visible = false;
            }
        }

        private void bt_Preview_Click(object sender, EventArgs e)
        {
            _PForm = this.Parent as DevExpress.XtraEditors.XtraForm;
            Assembly assembly = Assembly.GetExecutingAssembly();            // 实例化窗体
            DevExpress.XtraReports.UI.XtraReport _Report = assembly.CreateInstance(TS_Win_Class.GetSQlFieldValue("select f_str_1 from TS_S_T_P_Tree where f_id=" + _obj._sRMID, "f_str_1")) as DevExpress.XtraReports.UI.XtraReport;
            DataTable _dt = new DataTable();
            TS_Win_Class.FilledDataTable("select * from TS_S_T_H_RM_Result where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID, _dt);
            _Report.DataSource = _dt;
            DevExpress.XtraReports.UI.ReportPrintTool printTool = new DevExpress.XtraReports.UI.ReportPrintTool(_Report);
            printTool.ShowPreview();
        }

        private void bt_Print_Click(object sender, EventArgs e)
        {
            _PForm = this.Parent as DevExpress.XtraEditors.XtraForm;
            Assembly assembly = Assembly.GetExecutingAssembly();            // 实例化窗体
            DevExpress.XtraReports.UI.XtraReport _Report = assembly.CreateInstance(TS_Win_Class.GetSQlFieldValue("select f_str_1 from TS_S_T_P_Tree where f_id=" + _obj._sRMID, "f_str_1")) as DevExpress.XtraReports.UI.XtraReport;
            DataTable _dt = new DataTable();
            TS_Win_Class.FilledDataTable("select * from TS_S_T_H_RM_Result where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID, _dt);
            _Report.DataSource = _dt;
            DevExpress.XtraReports.UI.ReportPrintTool printTool = new DevExpress.XtraReports.UI.ReportPrintTool(_Report);
            printTool.Print();
        }

        private void bt_EXImg_Click(object sender, EventArgs e)
        {
            _PForm = this.Parent as DevExpress.XtraEditors.XtraForm;
            string _ReportFileName = TS_Win_Class.GetSQlFieldValue("select (select f_workcode from TS_TR_BU_Work_Main where f_id=TS_S_T_H_RM_Result.f_bmwid)+(select f_name from TS_S_T_P_Tree where f_id=TS_S_T_H_RM_Result.f_rmid) as f_name from TS_S_T_H_RM_Result where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID, "f_name");
            if (!Directory.Exists(TS_Win_Class._EXP_Direct))
            {
                Directory.CreateDirectory(TS_Win_Class._EXP_Direct);
            }
            if (File.Exists(TS_Win_Class._EXP_Direct + _ReportFileName + ".png"))
            {
                File.Delete(TS_Win_Class._EXP_Direct + _ReportFileName + ".png");
            }
            Assembly assembly = Assembly.GetExecutingAssembly();            // 实例化窗体
            DevExpress.XtraReports.UI.XtraReport _Report = assembly.CreateInstance(TS_Win_Class.GetSQlFieldValue("select f_str_1 from TS_S_T_P_Tree where f_id=" + _obj._sRMID, "f_str_1")) as DevExpress.XtraReports.UI.XtraReport;
            DataTable _dt = new DataTable();
            TS_Win_Class.FilledDataTable("select * from TS_S_T_H_RM_Result where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID, _dt);
            _Report.DataSource = _dt;
            _Report.ExportToImage(TS_Win_Class._EXP_Direct + _ReportFileName + ".png");
            MessageBox.Show("文件保存为" + TS_Win_Class._EXP_Direct + _ReportFileName + ".png");
        }

        private void bt_EXPDF_Click(object sender, EventArgs e)
        {
            _PForm = this.Parent as DevExpress.XtraEditors.XtraForm;
            string _ReportFileName = TS_Win_Class.GetSQlFieldValue("select (select f_workcode from TS_TR_BU_Work_Main where f_id=TS_S_T_H_RM_Result.f_bmwid)+(select f_name from TS_S_T_P_Tree where f_id=TS_S_T_H_RM_Result.f_rmid) as f_name from TS_S_T_H_RM_Result where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID, "f_name");
            if (!Directory.Exists(TS_Win_Class._EXP_Direct))
            {

                Directory.CreateDirectory(TS_Win_Class._EXP_Direct);

            }
            if (File.Exists(TS_Win_Class._EXP_Direct + _ReportFileName + ".pdf"))
            {
                File.Delete(TS_Win_Class._EXP_Direct + _ReportFileName + ".pdf");
            }
            Assembly assembly = Assembly.GetExecutingAssembly();            // 实例化窗体
            DevExpress.XtraReports.UI.XtraReport _Report = assembly.CreateInstance(TS_Win_Class.GetSQlFieldValue("select f_str_1 from TS_S_T_P_Tree where f_id=" + _obj._sRMID, "f_str_1")) as DevExpress.XtraReports.UI.XtraReport;
            DataTable _dt = new DataTable();
            TS_Win_Class.FilledDataTable("select * from TS_S_T_H_RM_Result where f_del=0 and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID, _dt);
            _Report.DataSource = _dt;
            _Report.ExportToPdf(TS_Win_Class._EXP_Direct + _ReportFileName + ".pdf");
            MessageBox.Show("文件保存为" + TS_Win_Class._EXP_Direct + _ReportFileName + ".pdf");
        }
        private void gf_data_TextChanged(object sender, EventArgs e)
        {
            TextBox _tb = new TextBox();
            try
            {
                string num = ((TextBox)sender).Text.Trim();
                string name = "";
                _tb = (TextBox)_PForm.Controls.Find(((TextBox)sender).Name.Substring(1), true)[0];
                if (num.Length > 0)
                {
                    name = TS_Win_Class.GetSQlFieldValue("select f_name from TS_S_T_P_Stuff where f_del=0 and f_workercode='" + num + "'", "f_name");
                    _tb.Text = name;
                }
                else
                {
                    _tb.Text = "";
                }
            }
            catch
            {

            }
        }
        private void bt_Announce_Click(object sender, EventArgs e)
        {
            //string _RPID = TS_Win_Class.GetSQlFieldValue("select f_id from TS_S_T_H_RM_Result where f_del=0 and f_type=" + _obj._sFunctionType + " and f_rmid=" + _obj._sRMID + " and f_bmwid=" + _obj._sBMWID, "f_id");
            //string _ReportName = TS_Win_Class.GetSQlFieldValue("select f_name from TS_S_T_P_Report_Main where f_id=" + _obj._sRMID, "f_name");
            //string _Name = TS_Win_Class.GetSQlFieldValue("select f_ttname+f_name as f_name from TS_TR_BU_Work_Main where f_id=" + _obj._sBMWID, "f_name");
            //if (_obj._sFunctionType == "1")
            //{
            //    _Name = TS_Win_Class.GetSQlFieldValue("select(select f_name from TS_S_T_P_TrainDepSys where f_id=TS_FI_BU_Work_Main.f_tdid)+(select f_code from TS_FI_B_Loco_Fittings_Base_List where f_id=TS_FI_BU_Work_Main.f_fiid) as f_name from TS_FI_BU_Work_Main where f_id=" + _obj._sBMWID, "f_name");
            //}
            //xuc_Train_Report_Announce_List _uc = new xuc_Train_Report_Announce_List();
            //_uc._LoadID = _RPID;
            //_uc._LoadRMID = _obj._sRMID;
            //_uc._LoadBMWID = _obj._sBMWID;
            //_uc._LoadType = _obj._sFunctionType;
            //_uc._LoadName = _Name;
            //_uc._LoadRName = _ReportName;
            //_uc.ShowDialog();
        }
    }
}
